<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="120px"
    >
      <el-form-item label="目的地" prop="destination">
        <el-input v-model="dataForm.destination" placeholder="目的地"></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="phoneNumber">
        <el-input v-model="dataForm.phoneNumber" placeholder="手机号"></el-input>
      </el-form-item>
      <el-form-item label="跟进人" prop="advisor">
        <!-- 所属分类 TODO -->
        <el-select v-model="dataForm.advisor" placeholder="跟进人">
          <el-option v-for="ad in AdvisorList" :key="ad.id" :label="ad.name" :value="ad.name" />
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import singleUpload from "@/components/upload/singleUpload";
export default {
  components: { singleUpload },
  data() {
    return {
      AdvisorList: [],
      visible: false,
      roleList: [],
      dataForm: {
        id: "",
        destination: "",
        phoneNumber: "",
        advisor: "",
      },
      pageIndex: 1,
      pageSize: 10
    };
  },
  created() {
    this.getAdvisorList();
  },
  methods: {
    //获取定制师列表
    getAdvisorList() {
      this.$http({
        url: this.$http.adornUrl(
          `/route/advisor/getAdvisor/${this.pageIndex}/${this.pageSize}`
        ),
        method: "get",
        params: this.$http.adornParams({}),
      }).then(({ data }) => {
        console.log("定制师列表", data);
        if (data && data.code === 20000) {
          this.AdvisorList = data.data.list;
        } else {
          this.AdvisorList = [];
        }
      });
    },
    //新增/修改
    init(id) {
      console.log("传过来的数据", id);
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        //新增则初始化数据
        this.$refs["dataForm"].resetFields();
        //修改则回显数据
        if (this.dataForm.id) {
          console.log(this.dataForm.id);
          this.$http({
            url: this.$http.adornUrl(
              `/demand/getDemandById/${this.dataForm.id}`
            ),
            method: "get",
            params: this.$http.adornParams(),
          }).then(({ data }) => {
            console.log(data.data);
            this.dataForm.destination = data.data.demand.destination;
            this.dataForm.phoneNumber = data.data.demand.phoneNumber;
            this.dataForm.advisor = data.data.demand.advisor;
          });
        }
      });
    },

    //提交表单
    dataFormSubmit() {
      this.$http({
        url: this.$http.adornUrl(
          `/demand/${!this.dataForm.id ? "addDemand" : "updateDemand"}`
        ),
        method: "post",
        data: this.$http.adornParams({
          id: this.dataForm.id || undefined,
          destination: this.dataForm.destination,
          phoneNumber: this.dataForm.phoneNumber,
          advisor: this.dataForm.advisor,
        }),
      }).then(({ data }) => {
        console.log("添加返回值", data);
        if (data && data.code === 20000) {
          //提示信息
          this.$message({
            message: "操作成功",
            type: "success",
            duration: 1500,
            onClose: () => {
              this.visible = false;
              this.$emit("refreshDataList");
            },
          });
        } else {
          this.$message.error("操作失败");
        }
      });
    },
  },
};
</script>